package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.course.activity.CourseDetailActivity;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.search.activity.SearchHomePageActivity;
import com.avic.rong.utils.DensityUtils;
import com.avic.rong.utils.GlideUtils;
import com.avic.rong.utils.StringUtils;
import com.bumptech.glide.Glide;
import com.example.minato.minastore.utils.AppUtils;
import com.orhanobut.logger.Logger;

import java.util.List;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 * 收缩版adapter
 */
public class SearchVpPeiXunRvAdapter extends RecyclerView.Adapter<SearchVpPeiXunRvAdapter.SearchPeiXunRvViewHolder>{

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> list;

    private static int itemCount = 3;

    public void addItemCount(int num) {
        itemCount = num;
    }

    public SearchVpPeiXunRvAdapter(Context context, List<GoodsSortSearchAllBzBean.DataBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public SearchPeiXunRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_search_peixun_rvitem,viewGroup,false);
        return new SearchPeiXunRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchPeiXunRvViewHolder holder, int i) {

        //设置标题筛选高亮
        if (list != null){
            SpannableString spannableString = new SpannableString(list.get(i).getTitle());
            if (list.get(i).getTitle() != null){
                int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
                if (startIndex>=0){
                    ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                    spannableString.setSpan(colorSpan, startIndex, startIndex+SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                    holder.title.setText(spannableString);
                }else {
                    holder.title.setText(list.get(i).getTitle());
                }
            }



          if (list.get(i).getCatIds()!=null) {
                if (list.get(i).getCatIds().get(0)!=null) {
                    Integer type = list.get(i).getCatIds().get(0);
                    String attrShow = list.get(i).getAttrShow();
                    String[] split = attrShow.split(",");
                    if (type==0){
                        //线上
                        //课长
                        String[] totaltime = split[1].split(":");
                        String totaltimeok = totaltime[1].substring(1, totaltime[1].length() - 1);

                        //课时
                        String[] Hourcount = split[3].split(":");
                        String Hourcountok = Hourcount[1].substring(0, Hourcount[1].length() - 1);

                        holder.peixun_class_hour.setText("课时："+Hourcountok+"课时");
                        holder.peixun_class_long.setText("课长："+totaltimeok);

                        GlideUtils.loaderHanldeRoundImage(context, list.get(i).getImg(), holder.img, R.mipmap.online_default, DensityUtils.dp2px(context, 6));


                    }else{
                        //线下
                        //地点
                        String[] city = split[0].split(":");
                        String cityok = city[1].substring(1, city[1].length() - 1);
                        //开始时间
                        String[] starttime = split[2].split(":");
                        String starttimeok = starttime[1].substring(0, starttime[1].length() - 2);
                        String[] qu00 = starttimeok.split(" ");
                        String s = qu00[0];
                        holder.peixun_class_hour.setText("地点："+cityok);
                        holder.peixun_class_long.setText("开始时间："+s);

                        GlideUtils.loaderHanldeRoundImage(context, list.get(i).getImg(), holder.img, R.mipmap.offline_default, DensityUtils.dp2px(context, 6));


                    }

                    // holder.desc.setText(list.get(i).getDesc());
                    holder.price.setText("¥" + list.get(i).getPrice() + "");
                    holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            StringBuffer sb = new StringBuffer();
                            Integer integer = list.get(i).getCatIds().get(0);
                            sb.append(integer);
                            long id = list.get(i).getDataId();
                            sb.append(id);
                            Logger.e("cjx", sb.toString());
                            if (StringUtils.isEmpty(list.get(i).getImg())) {
                                context.startActivity(CourseDetailActivity.getIntent(context, sb.toString(), "",""));
                            }else{
                                String substring = list.get(i).getImg().substring(49, list.get(i).getImg().length());
                                context.startActivity(CourseDetailActivity.getIntent(context, sb.toString(), substring,""));
                            }


                        }
                    });
                }
            }
        }
    }

    @Override
    public int getItemCount() {
//        if (list.size() > 3)
//            return itemCount;
//        else
            return list.size();
    }

    class SearchPeiXunRvViewHolder extends RecyclerView.ViewHolder{
        TextView title, desc, price,peixun_class_hour,peixun_class_long;
        ImageView img;
        LinearLayout linearLayout;
        public SearchPeiXunRvViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.goods_sort_ts_rv_img);
            price = itemView.findViewById(R.id.goods_sort_ts_rv_price);
            title = itemView.findViewById(R.id.goods_sort_ts_rv_title);
            desc = itemView.findViewById(R.id.goods_sort_ts_rv_desc);
            peixun_class_hour = itemView.findViewById(R.id.peixun_class_hour);
            peixun_class_long = itemView.findViewById(R.id.peixun_class_long);

            linearLayout = itemView.findViewById(R.id.goods_sort_ts_LinearLayout);

        }
    }
}
